﻿using System;
using System.Linq;
using ZilLion.Service.Core.DapperWrapper.SqlServer;
using ZilLion.Service.Core.Snowflake;
using ZilLion.Service.WeChatMall.Domain.Entitys.Cust;
using ZilLion.Service.WeChatMall.Domain.Repositorys.Interface;

namespace ZilLion.Service.WeChatMall.Domain.Repositorys
{
    public class CustReponsitory : SqlServerRespository<CustEntity>, ICustReponsitory
    {
        public CustReponsitory()
        {
            Context = new WechatMallContext();
        }


        public CustEntity GetCustEntity(string appid, string openid)
        {
            var entity =
                GetList(
                    @"SELECT CustId,AppId,OpenId,Addedtime from weisccustomer where appid=@appid and openid=@openid",
                    new {appid, openid}).FirstOrDefault();
            return entity;
        }

        public CustEntity GetCustEntity(long custid)
        {
            var entity =
                GetList(
                    @"SELECT CustId,AppId,OpenId,Addedtime from weisccustomer where custid=@custid",
                    new {custid}).FirstOrDefault();
            return entity;
        }

        public CustEntity AddCust(string appid, string openid)
        {
            var id = SnowflakeIdCreater.Instance().GetNextId();
            var weChatMallCustEntity =
                new CustEntity
                {
                    CustId = id,
                    AppId = appid,
                    OpenId = openid,
                    Addedtime = DateTime.Now.GetNowChinaTime()
                };
            Add(weChatMallCustEntity);
            return weChatMallCustEntity;
        }
    }
}